function promiseAll(promises) {
  return new Promise((resolve, reject) => {
    if (!Array.isArray(promises)) {
      throw new Error('must be array')
    }
    var resolveCounter = 0;
    var promiseNum = promises.length;
    var resolveResult = [];
    for (let i = 0; i < promiseNum; i++) {
      // 启动并发
      Promise.resolve(promises[i])
        .then(value => {
          resolveCounter++;
          resolveResult[i] = value;
          if (resolveCounter == promiseNum) {
            return resolve(resolveResult);
          }
        }, err => {
          return reject(err)
        })
    }
  })
}
// 测试用例  驱动写并正确写代码
let p1 = new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve(1)
  }, 1000)
})

let p2 = new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve(2)
  }, 2000)
})

let p3 = new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve(3)
  }, 3000)
})

promiseAll([p1, p2, p3])
  .then(res => {
    console.log(res)
  })
  .catch(err => {
    console.log(err)
  })